<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\StoreGoodsService;
use App\Services\DoctorListService;
use App\Services\DoctorGoodsCashService;
use App\Models\StoreCate;
use App\Models\StoreStyle;

class StoreGoodsController extends CommonController{
	private $StoreGoodsService;
    public function __construct(Request $request){
    	$this->StoreGoodsService = new StoreGoodsService();
    	$this->doctorListService = new DoctorListService();
    	$this->doctorGoodsCashService = new DoctorGoodsCashService();
        parent::__construct($request); 
    }
    
    /**
     * 列表
     */
    function index(){
    	$title = $this->request->input('title');
    	$cid = $this->request->input('cid');
    	$start_time = $this->request->input('start_time');
    	$end_time   = $this->request->input('end_time');
    	
    	$where = array();
    	if(isset($title) && $title !=''){
    		$where[] = array('title','like','%'.$title.'%');
    	}
    	if(isset($cid) && $cid !=''){
    		$where[] = array('cid','=',$cid);
    	}
    	
    	if(isset($start_time) && $start_time !=''){
    		$where[] = array('created_at','>=',$start_time);
    	}
    	
    	if(isset($end_time) && $end_time !=''){
    		$where[] = array('created_at','<=',$end_time);
    	}
    	
		$result = $this->StoreGoodsService->paginateInfo($where,$this->pageSize);
	
		$cateResult  = StoreCate::orderBy('sort','asc')->get();
		
    	return view('store.goods.index',['data'=>$result,'cateList'=>$cateResult]);
    }
    
    /**
     * 添加
     */
    function add(){
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('_token');
    		if(isset($param['style'])) 
    			$param['style'] = substr($param['style'],1);
    		else 
    			$param['style'] = '';
    		$result = $this->StoreGoodsService->addInfo($param);
    		
    		return response()->json($result);
    	}else{
    		$styleResult = StoreStyle::orderBy('sort','asc')->get();
    		$cateResult  = StoreCate::orderBy('sort','asc')->get();
    		return view('store.goods.add',['cateList'=>$cateResult,'styleList'=>$styleResult]);
    	}
    	
    }
    
    
    /**
     * 编辑
     */
    function edit(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('id','_token');
    		if(isset($param['style'])) 
    			$param['style'] = substr($param['style'],1);
    		else 
    			$param['style'] = '';
    		$result = $this->StoreGoodsService->updateInfo($where,$param);
    		return response()->json($result);
    	}else{
    		$styleResult = StoreStyle::orderBy('sort','asc')->get();
    		$cateResult = StoreCate::orderBy('sort','asc')->get();
    		$result = $this->StoreGoodsService->findInfo($where);
    		$result['data']['style'] = explode(',',$result['data']['style']);
    		
    		return view('store.goods.edit',['info'=>$result['data'],'cateList'=>$cateResult,'styleList'=>$styleResult]);
    	}
    }
    
    /*
     * 删除
     */
    function delete(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$result = $this->StoreGoodsService->deleteInfo($where);
    	return response()->json($result);
    }
    
    /**
     *上下架 
     */
    function updown(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$param['updown'] = $this->request->input('updown');
    	$result = $this->StoreGoodsService->updateInfo($where,$param);
    	return response()->json($result);
    }
    
    /**
     *上传图片 
     */
    function upload(Request $request){
//  	$path = $request->file('image')->store('avatars');
    	
        $file = $request->file('image');
        $uploadDir = "store" . '/' .date('Y-m-d');
        $path = $file->store($uploadDir, 'public');
		$path = '/storage/'.$path;
    	return response()->json(['code'=>1,'msg'=>'上传成功','data'=>$path]);
    }
    
    
    /**
     * 医生列表
     */
    function doctor(){
    	$goods_id = $this->request->input('gid');
    	$goodsResult = $this->StoreGoodsService->findInfo(array('id'=>$goods_id));
    	$goodsInfo   = $goodsResult['data'];
    	$result = $this->doctorListService->findAllInfo();
    	foreach($result['data'] as &$doctor){
    		$doctor['price'] = $goodsInfo['price'];
    		$goodsCashResult = $this->doctorGoodsCashService->findInfo(array('doctor_id'=>$doctor['id'],'goods_id'=>$goods_id));
    		if($goodsCashResult['code'] == 1){
    			$doctor['cash'] = $goodsCashResult['data']['cash'];
    		}else{
    			$doctor['cash'] = $goodsInfo['standard_cash'];
    		}
    	}
    	return view('store.goods.doctor',['data'=>$result,'goods_id'=>$goods_id]);
    }
    
    /**
     * 设置医生 特别佣金
     */
    function setCash(){
    	$goods_id = $this->request->input('goods_id');
    	$doctor_id = $this->request->input('doctor_id');
    	$cash = $this->request->input('cash');
    	$result = $this->doctorGoodsCashService->setCash($goods_id,$doctor_id,$cash);
    	return response()->json($result);
    }
}
